package com.digitalart.site.menu {
	import flash.display.DisplayObject;

	import com.digitalart.site.commons.DisplayObjectFinder;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import com.digitalart.site.commons.CustomEvent;

	import flash.events.Event;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;	

	/**
	 * @author johan.chaves
	 */
	public class MenuController extends EventDispatcher {
		
		public static const ON_MENU_READY : String = "onmenuready";
		public static const MENU_ITEM_CLICKED : String = "menuitemclicked";
		
		public static const HORIZONTAL_MENU_X_POS : Number = 0;
		public static const VERTICAL_MENU_X_POS : Number = 0;
		public static const HORIZONTAL_MENU_Y_POS : Number = -22;
		public static const VERTICAL_MENU_Y_POS : Number = 567;
				
		
		
		private var _menuModel : MenuModel;
		private var _horizontalMenuView : MenuView;
		private var _verticalMenuView : MenuView;
		
		public function MenuController() {
			
		}
		
		public function init() : void{
			
			_menuModel = new MenuModel();
			_menuModel.addEventListener(MenuModel.ON_LOAD_DATA_READY, onDataReady);
			_menuModel.init(); 
		}
		
		private function onDataReady(event : Event) : void {
			
			event.target.removeEventListener(MenuModel.ON_LOAD_DATA_READY, onDataReady);
			_horizontalMenuView = new MenuView();
			_horizontalMenuView.addEventListener(MenuView.ON_MENU_VIEW_READY, onHorizontalMenuReady);
			setHorizontalMenuProperties();
		}
		
		private function setHorizontalMenuProperties() : void{
			
			setMenuItems(_menuModel.horizontalMenuItems, _horizontalMenuView.items);
			_horizontalMenuView.x = HORIZONTAL_MENU_X_POS;
			_horizontalMenuView.y = HORIZONTAL_MENU_Y_POS;
			_horizontalMenuView.type = MenuView.HORIZONTAL_TYPE;			
			_horizontalMenuView.init();
		}		

		private function setVerticalMenuProperties() : void{
			
			setMenuItems(_menuModel.verticalMenuItems, _verticalMenuView.items);
			_verticalMenuView.x = VERTICAL_MENU_X_POS;
			_verticalMenuView.y = VERTICAL_MENU_Y_POS;
			_verticalMenuView.type = MenuView.VERTICAL_TYPE;			
			_verticalMenuView.init();
		}				
		
		private function setMenuItems(itemModelList : Array,itemViewList : Array) : void {
			
			var menuItem : MenuItem;
			for each(var itemVO : MenuItemVO in itemModelList) {				
				menuItem = new MenuItem(itemVO.id, itemVO.text, itemVO.type);				
				menuItem.addEventListener(MouseEvent.CLICK, menuItemClicked);
				menuItem.addEventListener(MouseEvent.MOUSE_OVER, menuItemOver);
				menuItem.addEventListener(MouseEvent.MOUSE_OUT, menuItemOut);
				itemViewList.push(menuItem);
			}
		}

		private function menuItemOut(event : MouseEvent) : void {
			(event.currentTarget as MenuItem).setTextColorOnRollOut();
		}

		private function menuItemOver(event : MouseEvent) : void {			
			(event.currentTarget as MenuItem).setTextColorOnRollOver();
		}

		private function menuItemClicked(event : MouseEvent) : void {	
					
			_verticalMenuView.setItemsUnselected();
			_horizontalMenuView.setItemsUnselected();
			(event.currentTarget as MenuItem).setTextColorSelected();
			(event.currentTarget as MenuItem).isSelected = true;
			
			var args : Array = new Array();
			args.push(event.currentTarget.id);
			args.push(event.currentTarget.type);
			
			var e : CustomEvent = new CustomEvent(MENU_ITEM_CLICKED, args);					
			dispatchEvent(e);
		}

		private function onHorizontalMenuReady(event : Event) : void {
			
			event.target.removeEventListener(MenuView.ON_MENU_VIEW_READY, onHorizontalMenuReady);
			_verticalMenuView = new MenuView();
			_verticalMenuView.addEventListener(MenuView.ON_MENU_VIEW_READY, onVerticalMenuReady);
			setVerticalMenuProperties();
		}
		
		private function onVerticalMenuReady(event : Event) : void {
			
			event.target.removeEventListener(MenuView.ON_MENU_VIEW_READY, onVerticalMenuReady);
			addEventsForAnimation();							
			dispatchEvent(new Event(ON_MENU_READY));
		}		
			
		public function addEventsForAnimation():void {		
							
			var header : MovieClip = _horizontalMenuView.getHeader();		
			header.addEventListener(MouseEvent.CLICK, menuClicked);
			header.mouseChildren = false;
			header.buttonMode = true;
			
			header = _verticalMenuView.getHeader();		
			header.addEventListener(MouseEvent.CLICK, menuClicked);
			header.mouseChildren = false;
			header.buttonMode = true;
		}

		private function menuClicked(event : MouseEvent) : void {
			var menu : MenuView = MenuView(DisplayObjectFinder.getParent(DisplayObject(event.target), MenuView));
			menu.showEffect();			
		}
		
		public function unSelectAllItems() : void {
			_verticalMenuView.setItemsUnselected();
			_horizontalMenuView.setItemsUnselected();
		}
		
		public function enableItems() : void {
			
			enableMenuItems(_horizontalMenuView.items);
			enableMenuItems(_verticalMenuView.items);
		}
		
		public function disableItems() : void {
			
			disableMenuItems(_horizontalMenuView.items);
			disableMenuItems(_verticalMenuView.items);
		}
		
		private function disableMenuItems(items : Array) : void{
						
			for each(var item : MenuItem in items) {								
				item.removeEventListener(MouseEvent.CLICK, menuItemClicked);
				item.removeEventListener(MouseEvent.MOUSE_OVER, menuItemOver);
				item.removeEventListener(MouseEvent.MOUSE_OUT, menuItemOut);				
			}
		}
		
		private function enableMenuItems(items : Array) : void{
						
			for each(var item : MenuItem in items) {								
				item.addEventListener(MouseEvent.CLICK, menuItemClicked);
				item.addEventListener(MouseEvent.MOUSE_OVER, menuItemOver);
				item.addEventListener(MouseEvent.MOUSE_OUT, menuItemOut);				
			}
		}
		
		public function getHorizontalMenu() : Sprite{
			return _horizontalMenuView;
		}
		public function getVerticalMenu() : Sprite{
			return _verticalMenuView;
		}
	}
}



